home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / wb-enhancement / clocktool / clocktool.c < prev    next >
C/C++ Source or Header  |  1995-11-06  |  31KB  |  1,743 lines

  1. #define VERSION "2.1 "
  2.  
  3. #define EARLIEST_LIB    37    /* 2.04 and above */
  4. /* *INDENT-OFF* */
  5. /**
  6.  
  7.  * ClockTool.c
  8.  *
  9.  *  Displays/adjusts/sets times of battery clock and/or system clock
  10.  *  from one or the other.
  11.  *
  12.  * Options:
  13.  *
  14.  *      -b       ; battery clock
  15.  *      -s       ; system  clock
  16.  *      -d<str>  ; set selected clock to str (dd-MMM-yy)
  17.  *      -t<str>  ; set selected clock to str (hh:mm[:ss])
  18.  *                    (use either or both d/t options)
  19.  *      -annnn   ; adjust selected clock time by -nnnn millisecs",
  20.  *      +annnn   ; adjust selected clock time by +nnnn millisecs",
  21.  *      -l<file> ; log system time to <file> , reset clock from other
  22.  *                                    (default LOG_CLOCKTOOL:)
  23.  *      -L<file> ; log system time to <file> , Dont reset clock from other
  24.  *                                    (default LOG_CLOCKTOOL:)
  25.  *      -n<str>  ; append the <str> to log file entry, e.g. "reboot"
  26.  *
  27.  *  (see Usage () )
  28.  *
  29.  * Runs under AmigaDOS 2.04+, and only machines containing
  30.  * a battery clock.
  31.  *
  32.  *
  33.  *
  34.  *****  Written by Gary Duncan
  35.  *
  36.  *      57 Melbourne Hill Rd
  37.  *      Warrandyte
  38.  *      Vic 3113
  39.  *      Australia
  40.  *
  41.  * E-mail: gduncan@werple.net.au
  42.  *
  43.  *
  44.  *****  Freely distributable for non-commercial purposes.
  45.  *      Please keep this header intact.
  46.  *
  47.  *
  48.  *      Compiles under SAS 6.2
  49.  *
  50.  *      Formatted with 'indent -gnu' ; a big time-saving program.
  51.  *
  52.  * Functions:-
  53.  
  54.     main
  55.     tv2dss
  56.     tv2str
  57.     dss2tv
  58.     dss2str
  59.     str2dss
  60.     Init_Timer
  61.     hexit
  62.     DeleteTimer
  63.     usage
  64.     scan_args
  65.     tv_diff
  66.     tv_diff_str
  67.     format_diff_str
  68.     set_system_time
  69.     get_system_time
  70.     get_battclock_time
  71.     set_battclock_time
  72.     tv_update
  73.     check4battclock
  74.     get_syncd_battclock_time
  75.     get_syncd_system_time
  76.     print_clock
  77.     display_clocks
  78.     adjust_clock
  79.     set_system_time_to_syncd_battclock
  80.     set_battclock_time_to_syncd_system_time
  81.     wait_for_timer
  82.     pause
  83.  
  84.  
  85.  
  86.  Version        Date    Changes
  87.  ~~~~~~~        ~~~~    ~~~~~~~
  88.  
  89.  1.0            27Oct93 Original release
  90.  1.1            04Apr94 Changes :-
  91.                         1) use __AMIGADATE__
  92.                         2) added L option, reorganised logic.
  93.                         3) added d,t options (set date/time).
  94.                         4) corrected clocktool -b output for big difference  
  95.  1.1a           Comments added
  96.  
  97.  2.0        18Apr95    Exit with good/bad (0/1) values
  98.  2.1        26Oct95 trivial changes to help
  99.  **/
  100.  
  101. /* *INDENT-ON* */
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. #include "ClockTool.h"
  109.  
  110.  
  111. /*
  112.  * string for AmigaDOS Version Command
  113.  */
  114. char A_vers[] = "\0$VER: clocktool\t" VERSION __AMIGADATE__;
  115.  
  116.  
  117. LIBRARY *UtilityBase = NULL;
  118. LIBRARY *BattClockBase;
  119.  
  120. TIMEVAL tv_sysclock;
  121. TIMEVAL tv_battclock;
  122. TIMEVAL tv_tmp;
  123.  
  124. DATESTAMP dss_sysclock;
  125. DATESTAMP dss_battclock;
  126.  
  127. DATETIME datetime;
  128. char *date_ptr;
  129. char *time_ptr;
  130.  
  131. LONG adjust;
  132. LONG temp;
  133.  
  134. MSGPORT *TimeP = NULL;
  135. TIMERREQUEST *Timermsg;
  136. IOREQUEST *Timer = NULL;
  137.  
  138. BOOL a_flag = FALSE;
  139. BOOL b_flag = FALSE;
  140. BOOL d_flag = FALSE;
  141. BOOL l_flag = FALSE;
  142. BOOL L_flag = FALSE;
  143. BOOL n_flag = FALSE;
  144. BOOL s_flag = FALSE;
  145. BOOL t_flag = FALSE;
  146.  
  147. char buf[80];
  148. char *xstr = "";
  149.  
  150. int opt_count = 0;
  151.  
  152. char *logfile;
  153. FILE *fp = NULL;
  154. char log_buf[80];
  155.  
  156. /*
  157.  * used by date conversion functions
  158.  */
  159.  
  160. DM days_month[] =
  161. {
  162.   31, "Jan",
  163.   28, "Feb",
  164.   31, "Mar",
  165.   30, "Apr",
  166.   31, "May",
  167.   30, "Jun",
  168.   31, "Jul",
  169.   31, "Aug",
  170.   30, "Sep",
  171.   31, "Oct",
  172.   30, "Nov",
  173.   31, "Dec",
  174. };
  175.  
  176. /*
  177.  *********************************************************************
  178.  */
  179.  
  180. VOID
  181. main (int argc, char **argv)
  182. {
  183.  
  184.   /*
  185.    * abort if no battclock and not AmigaDos 2.xx
  186.    */
  187.   if (check4battclock () == FALSE)
  188.     {
  189.       hexit (1);
  190.     }
  191.  
  192.   scan_args (argc, argv);
  193.  
  194.   if (Init_Timer () == FALSE)
  195.     {
  196.       hexit (1);
  197.     }
  198.  
  199.   /*
  200.    ***** Mode (1) ? - just display both clocks ?
  201.    */
  202.   if (opt_count == 0)
  203.     {
  204.       get_syncd_battclock_time (&tv_battclock);
  205.       get_system_time (&tv_sysclock);
  206.  
  207.       display_clocks (&tv_battclock, &tv_sysclock);
  208.       hexit (0);
  209.     }
  210.  
  211.   /*
  212.    ***** Mode (2) -  just a s or b option ?
  213.    */
  214.  
  215.   if (opt_count == 1)
  216.     {
  217.       /*
  218.        * just set one clock from the other
  219.        */
  220.       if (s_flag == TRUE)
  221.     {
  222.       (void) set_system_time_to_syncd_battclock ();
  223.  
  224.       print_old_new ("system", &tv_sysclock, &tv_battclock);
  225.     }
  226.       else
  227.     /* set battclock from system time */
  228.     {
  229.       (void) set_battclock_time_to_syncd_system_time ();
  230.  
  231.       print_old_new ("battery", &tv_battclock, &tv_sysclock);
  232.     }
  233.  
  234.       hexit (0);
  235.     }
  236.  
  237.   /*
  238.    ***** Mode (3) -  set date/time from command line ?
  239.    */
  240.  
  241.   if ((d_flag == TRUE) || (t_flag == TRUE))
  242.     {
  243.       TIMEVAL t_val;
  244.  
  245.       DateStamp (&datetime.dat_Stamp);
  246.  
  247.       datetime.dat_Format = FORMAT_DOS;
  248.       datetime.dat_Flags = 0;
  249.  
  250.       if (d_flag)
  251.     {
  252.       datetime.dat_StrDate = date_ptr;
  253.       datetime.dat_StrTime = NULL;
  254.       if (StrToDate (&datetime) == FALSE)
  255.         {
  256.           fprintf (stderr, "invalid date string\n");
  257.           hexit (1);
  258.         }
  259.     }
  260.       if (t_flag)
  261.     {
  262.       datetime.dat_StrTime = time_ptr;
  263.       datetime.dat_StrDate = NULL;
  264.       if (StrToDate (&datetime) == FALSE)
  265.         {
  266.           fprintf (stderr, "invalid time string\n");
  267.           hexit (1);
  268.         }
  269.     }
  270.  
  271.       /*
  272.        * now change date/time of selected clock
  273.        */
  274.       dss2tv (&datetime.dat_Stamp, &t_val);
  275.  
  276.       b_flag ? set_battclock_time (&t_val) : set_system_time (&t_val);
  277.  
  278.       /*
  279.        * now display both clocks 
  280.        */
  281.       get_syncd_battclock_time (&tv_battclock);
  282.       get_system_time (&tv_sysclock);
  283.  
  284.       display_clocks (&tv_battclock, &tv_sysclock);
  285.       hexit (0);
  286.  
  287.     }
  288.  
  289.   /*
  290.    ***** Mode (4) - adjust a clock (no logging) ?
  291.    */
  292.  
  293.   if ((a_flag == TRUE) && (opt_count == 2))
  294.     {
  295.       adjust_clock (adjust, &tv_tmp);
  296.  
  297.       if (s_flag == TRUE)
  298.     {
  299.       print_old_new ("system", &tv_sysclock, &tv_tmp);
  300.     }
  301.       else
  302.     {
  303.       print_old_new ("battery", &tv_battclock, &tv_tmp);
  304.     }
  305.       hexit (0);
  306.     }
  307.  
  308.   /*
  309.    ***** Mode (5) - log selected clock time with reset  ?
  310.    */
  311.  
  312.   if (l_flag == TRUE)
  313.     {
  314.       if (s_flag == TRUE)
  315.     {
  316.       temp = set_system_time_to_syncd_battclock ();
  317.  
  318.       sprintf (log_buf, "\nS: %s %s",
  319.            tv2str (&tv_sysclock),
  320.            format_diff_str (temp, "(%s secs)  "));
  321.     }
  322.       else if (b_flag == TRUE)
  323.     {
  324.       temp = set_battclock_time_to_syncd_system_time ();
  325.  
  326.       sprintf (log_buf, "\nB: %s %s",
  327.            tv2str (&tv_battclock),
  328.            format_diff_str (temp, "(%s secs)  "));
  329.     }
  330.  
  331.       if (n_flag == TRUE)
  332.     {
  333.       strcat (log_buf, xstr);
  334.     }
  335.  
  336.       printf ("%s\n", log_buf);
  337.     }
  338.  
  339.   /*
  340.    ***** Mode (6) - log selected clock time with NO reset  ?
  341.    */
  342.  
  343.   if (L_flag == TRUE)
  344.     {
  345.  
  346.       get_syncd_battclock_time (&tv_battclock);
  347.       get_system_time (&tv_sysclock);
  348.  
  349.       temp = tv_diff (&tv_sysclock, &tv_battclock);
  350.  
  351.       if (s_flag == TRUE)
  352.     {
  353.       sprintf (log_buf, "\nS: %s %s", tv2str (&tv_sysclock),
  354.            format_diff_str (temp, "(%s secs)  "));
  355.     }
  356.       else if (b_flag == TRUE)
  357.     {
  358.       sprintf (log_buf, "\nB: %s %s", tv2str (&tv_battclock),
  359.            format_diff_str (temp, "(%s secs)  "));
  360.  
  361.     }
  362.  
  363.       if (n_flag == TRUE)
  364.     {
  365.       strcat (log_buf, xstr);
  366.     }
  367. #if 1
  368.       printf ("%s\n", log_buf);
  369. #endif
  370.     }
  371.  
  372.   /*
  373.    * now log message , maybe
  374.    */
  375.   if (fp)
  376.     {
  377.       int rote;
  378.       int len = strlen (log_buf);
  379.  
  380.       if ((rote = fwrite (log_buf, 1, len, fp)) != len)
  381.     {
  382.       fprintf (stderr, "fwrite error (wanted=%d),(actual=%d)\n",
  383.            len, rote);
  384.  
  385.       hexit (99);
  386.     }
  387.     }
  388.  
  389.   hexit (0);
  390. }
  391.  
  392.  
  393. /***************************************************************************
  394.  
  395.  
  396.  Function :     tv2dss
  397.  
  398.  Purpose:       given a Timeval structure , returns Datestamp
  399.  
  400.  Entry    :     ptr to Timeval
  401.  ptr to Datestamp
  402.  
  403.  Returns  :
  404.  
  405.  
  406.  ****************************************************************************/
  407.  
  408.  
  409. void
  410. tv2dss (TIMEVAL * tv, DATESTAMP * dss)
  411.  
  412. {
  413.   long rem;
  414.  
  415.   dss->ds_Days = tv->tv_secs / (24 * 60 * 60);
  416.   rem = tv->tv_secs % (24 * 60 * 60);    /* secs in last day */
  417.  
  418.   dss->ds_Minute = rem / 60;
  419.   rem = rem % 60;        /* secs in last minute */
  420.  
  421.   rem = (rem * 1000) + (tv->tv_micro / 1000);    /* msecs in last minute */
  422.  
  423.   dss->ds_Tick = rem / (1000 / TICKS_PER_SECOND);    /* ticks in last minute */
  424. }
  425.  
  426. /***************************************************************************
  427.  
  428.  
  429.  Function :     tv2str
  430.  
  431.  Purpose:       given a Timeval structure , returns date string
  432.  
  433.  Entry    :     ptr to Timeval
  434.  
  435.  Returns  :
  436.  
  437.  
  438.  ****************************************************************************/
  439.  
  440.  
  441. char *
  442. tv2str (TIMEVAL * tv)
  443.  
  444. {
  445.   static char tbuf[40];
  446.   DATESTAMP dss;
  447.  
  448.   tv2dss (tv, &dss);
  449.   dss2str (&dss, tbuf);
  450.   return tbuf;
  451. }
  452.  
  453. /***************************************************************************
  454.  
  455.  
  456.  Function :     dss2tv
  457.  
  458.  Purpose:       given a DateStamp structure , returns an updated TIMEVAL
  459.  
  460.  Entry    :    ptr to DateStamp structure
  461.  
  462.  Returns  :
  463.  
  464.  
  465.  ****************************************************************************/
  466.  
  467.  
  468. void
  469. dss2tv (DATESTAMP * t1, TIMEVAL * tv)
  470.  
  471. {
  472.   ULONG secs;
  473.  
  474.   secs = t1->ds_Days * 24 * 60 * 60;
  475.   secs += t1->ds_Minute * 60;
  476.   secs += t1->ds_Tick / TICKS_PER_SECOND;
  477.  
  478.   tv->tv_secs = secs;
  479.   tv->tv_micro = (t1->ds_Tick % TICKS_PER_SECOND) * (1000000 / TICKS_PER_SECOND);
  480.  
  481. }
  482.  
  483. /***************************************************************************
  484.  
  485.  
  486.  Function :     dss2str
  487.  
  488.  Purpose:       generates a date string given a DateStamp
  489.  
  490.  Entry    :     ptr to Datestamp
  491.  
  492.  
  493.  Returns  :    ptr to date string
  494.  
  495.  
  496.  
  497.  ****************************************************************************/
  498.  
  499.  
  500. void
  501. dss2str (DATESTAMP * dss, char *str)
  502.  
  503. {
  504.   static TIMEVAL tv;
  505.   struct tm Time;
  506.   int j;
  507.   long time, ago;
  508.  
  509.  
  510.   dss2tv (dss, &tv);        /* date to secs/usecs since 01 Jan 78 */
  511.   time = tv.tv_secs;
  512.  
  513.   Time.tm_sec = time % 60;
  514.   time /= 60;
  515.   Time.tm_min = time % 60;
  516.   time /= 60;
  517.   Time.tm_hour = time % 24;
  518.   time /= 24;
  519.   Time.tm_wday = time % 7;
  520.   Time.tm_year = 78 + (time / (4 * 365 + 1)) * 4;
  521.   time %= 4 * 365 + 1;
  522.  
  523.  
  524.   while (time)
  525.     {
  526.       ago = 365;
  527.       if ((Time.tm_year & 3) == 0)    /* leap year */
  528.     ago++;
  529.       if (time < ago)
  530.     break;
  531.       time -= ago;
  532.       Time.tm_year++;
  533.     }
  534.   Time.tm_yday = ++time;
  535.  
  536.   for (j = 0; j < 12; j++)
  537.     {
  538.       ago = days_month[j].days;
  539.       if (j == 1 && (Time.tm_year & 3) == 0)    /* Feb , and leap year ? */
  540.     ago++;
  541.       if (time <= ago)
  542.     break;
  543.       time -= ago;
  544.     }
  545.   Time.tm_mon = j;
  546.   Time.tm_mday = time;
  547.  
  548.   /* date format : 23-dec-88 22:33:01  */
  549.  
  550.   sprintf (str, "%02d-%s-%2d %02d:%02d:%02d.%03d",
  551.        Time.tm_mday,
  552.        days_month[Time.tm_mon].months,
  553.        Time.tm_year,
  554.        Time.tm_hour,
  555.        Time.tm_min,
  556.        Time.tm_sec,
  557.        tv.tv_micro / 1000
  558.     );
  559.  
  560. }
  561.  
  562. /***************************************************************************
  563.  
  564.  
  565.  Function :   str2dss
  566.  
  567.  
  568.  Purpose  :   converts a truncated Amiga date string to a Datestamp
  569.  
  570.  e.g.   "22-Dec-92 08:10:26" , no leading day
  571.  
  572.  Entry    :
  573.  
  574.  
  575.  Returns  :   TRUE if successful, FALSE otherwise
  576.  
  577.  
  578.  
  579.  ****************************************************************************/
  580.  
  581. BOOL
  582. str2dss (char *str, DATESTAMP * ds)
  583.  
  584. {
  585. /* 012345678901234567 */
  586. /* 22-Dec-92 08:10:26 */
  587.  
  588.   char *p = str;
  589.   ULONG dss_days;
  590.  
  591.   int day, month, year, hour, min, sec, diy, j, k, rem;
  592.  
  593.   p[2] = '\0';
  594.   p[6] = '\0';
  595.   p[9] = '\0';
  596.   p[12] = '\0';
  597.   p[15] = '\0';
  598.  
  599.   day = atoi (&p[0]);
  600.   year = atoi (&p[7]) + 1900;
  601.   hour = atoi (&p[10]);
  602.   min = atoi (&p[13]);
  603.   sec = atoi (&p[16]);
  604.  
  605.   /*
  606.    * compute days-in-year from month
  607.    * - may be incremented later if a leap year
  608.    */
  609.   for (p = str + 3, j = 0, diy = 0;; ++j)
  610.     {
  611.       if (j == 12)
  612.     return FALSE;
  613.       if (strcmp (p, days_month[j].months) == 0)
  614.     {
  615.       month = j + 1;
  616.       break;
  617.     }
  618.       diy += days_month[j].days;
  619.     }
  620.  
  621.   /*
  622.    * tricky bit ; adjust for leap years
  623.    */
  624.   dss_days = diy + day - 1;
  625.   dss_days += 2 * 365;        /* 1978, 1979 */
  626.   for (j = 1980, k = 0;;)
  627.     {
  628.       dss_days += 365;
  629.       if ((rem = (k++ % 4)) == 0)
  630.     dss_days += 1;        /* leap year */
  631.  
  632.       if (++j == year)
  633.     {
  634.       if (((rem = (k++ % 4)) == 0) && (month > 2))
  635.         {
  636.           dss_days += 1;
  637.         }
  638.       break;
  639.     }
  640.     }
  641.  
  642.   ds->ds_Days = dss_days;
  643.   /*
  644.    * now fill in mins, ticks in min
  645.    */
  646.   ds->ds_Minute = (60 * hour) + min;
  647.   ds->ds_Tick = sec * TICKS_PER_SECOND;
  648.  
  649.   return TRUE;
  650. }
  651.  
  652. /***************************************************************************
  653.  
  654.  
  655.  Function :     Init_Timer
  656.  
  657.  Purpose:       Inits timer
  658.  
  659.  Entry    :
  660.  
  661.  
  662.  Returns  :
  663.  
  664.  
  665.  
  666.  ****************************************************************************/
  667.  
  668.  
  669. BOOL
  670. Init_Timer (void)
  671. {
  672.  
  673.   int error;
  674.  
  675.   TimeP = CreatePort (0, 0);
  676.   if (TimeP == NULL)
  677.     return FALSE;
  678.  
  679.   Timermsg = (TIMERREQUEST *) CreateExtIO (TimeP, sizeof (TIMERREQUEST));
  680.   if (Timermsg == NULL)
  681.     return FALSE;
  682.  
  683.   error = OpenDevice (TIMERNAME, UNIT_MICROHZ, (IOREQUEST *) Timermsg, 0);
  684.   if (error != 0)
  685.     {
  686.       DeleteExtIO ((IOREQUEST *) Timermsg);
  687.       return FALSE;
  688.     }
  689.   return TRUE;
  690. }
  691.  
  692.  
  693. /***************************************************************************
  694.  
  695.  
  696.  Function :      hexit
  697.  
  698.  
  699.  Purpose  :      cleans up and exits
  700.  
  701.  
  702.  Entry    :
  703.  
  704.  
  705.  Returns  :
  706.  
  707.  
  708.  
  709.  ****************************************************************************/
  710.  
  711.  
  712. void
  713. hexit (int exit_val)
  714. {
  715.  
  716.   if (UtilityBase)
  717.     {
  718.       CloseLibrary (UtilityBase);
  719.     }
  720.  
  721.   if (Timermsg != NULL)
  722.     {
  723.       DeleteTimer (Timermsg);
  724.     }
  725.   /*
  726.    * close log file if open
  727.    */
  728.   if (fp)
  729.     fclose (fp);
  730.  
  731.   exit (exit_val);
  732. }
  733.  
  734. /***************************************************************************
  735.  
  736.  
  737.  Function :     DeleteTimer
  738.  
  739.  Purpose:       Deletes Timer
  740.  
  741.  Entry    :
  742.  
  743.  
  744.  Returns  :
  745.  
  746.  
  747.  
  748.  ****************************************************************************/
  749.  
  750.  
  751. void
  752. DeleteTimer (TIMERREQUEST * tr)
  753. {
  754.   MSGPORT *tp;
  755.  
  756.   tp = tr->tr_node.io_Message.mn_ReplyPort;
  757.   if (tr != 0)
  758.     {
  759.       CloseDevice ((IOREQUEST *) tr);
  760.       DeleteExtIO ((IOREQUEST *) tr);
  761.     }
  762.   if (tp != 0)
  763.     DeletePort (tp);
  764. }
  765.  
  766. /***************************************************************************
  767.  
  768.  
  769.  Function :     tv_diff
  770.  
  771.  
  772.  Purpose  :     returns difference in msecs between two TIMEVALS,
  773.  or -1 if overflow 
  774.  Entry    :
  775.  
  776.  
  777.  Returns  :
  778.  
  779.  
  780.  
  781.  ****************************************************************************/
  782.  
  783. LONG
  784. tv_diff (TIMEVAL * tv1, TIMEVAL * tv2)
  785. {
  786.   LONG val;
  787.  
  788.   val = (LONG) tv2->tv_secs - (LONG) tv1->tv_secs;
  789.   /*
  790.    * quick and nasty range check
  791.    */
  792.   if (labs (val) > 4294966L)
  793.     return -1;
  794.  
  795.   val *= 1000;
  796.  
  797.   val += (((LONG) tv2->tv_micro) - ((LONG) tv1->tv_micro)) / 1000;
  798.  
  799.   return val;
  800. }
  801.  
  802.  
  803. /***************************************************************************
  804.  
  805.  
  806.  Function :     tv_diff_str
  807.  
  808.  
  809.  Purpose  :     returns difference in msecs between two TIMEVALS
  810.  expressed as secs.msecs string
  811.  
  812.  Entry    :
  813.  
  814.  
  815.  Returns  :
  816.  
  817.  
  818.  
  819.  ****************************************************************************/
  820.  
  821. char *
  822. tv_diff_str (TIMEVAL * tv1, TIMEVAL * tv2)
  823. {
  824.   LONG val;
  825.  
  826.   val = tv_diff (tv1, tv2);
  827.  
  828.   return (format_diff_str (val, " (%s)"));
  829. }
  830.  
  831. /***************************************************************************
  832.  
  833.  
  834.  Function :     format_diff_str
  835.  
  836.  
  837.  Purpose  :     formats diff string
  838.  
  839.  Entry    :
  840.  
  841.  
  842.  Returns  :
  843.  
  844.  
  845.  
  846.  ****************************************************************************/
  847.  
  848. char *
  849. format_diff_str (LONG val, char *form)
  850. {
  851.   static char mbuf[80];
  852.   char tmp[80];
  853.  
  854.   tmp[0] = ' ';
  855.  
  856.   if (val == -1)
  857.     {
  858.       return " ( >999.999 ) ";
  859.     }
  860.  
  861.   if (val < 0)
  862.     {
  863.       tmp[0] = '-';
  864.       val = -val;
  865.     }
  866.   else if (val > 0)
  867.     {
  868.       tmp[0] = '+';
  869.     }
  870.   if (val > 999999)
  871.     {
  872.       return " ( >999.999 ) ";
  873.     }
  874.  
  875.   sprintf (&tmp[1], " %3d.%03d", val / 1000, val % 1000);
  876.  
  877.   sprintf (mbuf, form, tmp);
  878.   return mbuf;
  879. }
  880.  
  881. /***************************************************************************
  882.  
  883.  
  884.  Function :     set_system_time
  885.  
  886.  Purpose:       sets system date/time
  887.  
  888.  Entry    :     TIMEVAL
  889.  
  890.  
  891.  Returns  :     void
  892.  
  893.  
  894.  ****************************************************************************/
  895.  
  896. void
  897. set_system_time (TIMEVAL * tv)
  898. {
  899.  
  900.   Timermsg->tr_node.io_Command = TR_SETSYSTIME;
  901.   Timermsg->tr_time.tv_secs = tv->tv_secs;
  902.   Timermsg->tr_time.tv_micro = tv->tv_micro;
  903.  
  904.   if (DoIO ((IOREQUEST *) Timermsg))
  905.     {
  906.       fprintf (stderr, "set_system_time() - DoIO fail\n");
  907.       hexit (1);
  908.     }
  909. }
  910.  
  911. /***************************************************************************
  912.  
  913.  
  914.  Function :     get_system_time
  915.  
  916.  Purpose:       gets system date/time
  917.  
  918.  Entry    :     ptr to TIMEVAL
  919.  
  920.  
  921.  Returns  :     void
  922.  
  923.  
  924.  ****************************************************************************/
  925.  
  926. void
  927. get_system_time (TIMEVAL * tv)
  928. {
  929.  
  930.   Timermsg->tr_node.io_Command = TR_GETSYSTIME;
  931.  
  932.   if (DoIO ((IOREQUEST *) Timermsg))
  933.     {
  934.       fprintf (stderr, "get_time() - DoIO fail\n");
  935.       hexit (1);
  936.     }
  937.   tv->tv_secs = Timermsg->tr_time.tv_secs;
  938.   tv->tv_micro = Timermsg->tr_time.tv_micro;
  939. }
  940.  
  941. /***************************************************************************
  942.  
  943.  
  944.  Function :     get_battclock_time
  945.  
  946.  Purpose:       Get Battery Backed-up Clock time
  947.  
  948.  Entry    :    ptr to TIMEVAL structure
  949.  
  950.  Returns  :
  951.  
  952.  
  953.  ****************************************************************************/
  954.  
  955.  
  956. void
  957. get_battclock_time (TIMEVAL * tv)
  958.  
  959. {
  960.  
  961.   tv->tv_secs = ReadBattClock ();
  962.   tv->tv_micro = 0;
  963. }
  964.  
  965. /***************************************************************************
  966.  
  967.  
  968.  Function :     set_battclock_time
  969.  
  970.  Purpose:       Set Battery Backed-up Clock time
  971.  
  972.  Entry    :    ptr to TIMEVAL structure
  973.  
  974.  Returns  :
  975.  
  976.  
  977.  ****************************************************************************/
  978.  
  979.  
  980. void
  981. set_battclock_time (TIMEVAL * tv)
  982.  
  983. {
  984.   WriteBattClock (tv->tv_secs);
  985. }
  986.  
  987. /***************************************************************************
  988.  
  989.  
  990.  Function :     tv_update
  991.  
  992.  
  993.  Purpose  :     updates a TIMEVAL with nnn msecs
  994.  
  995.  Entry    :
  996.  
  997.  
  998.  Returns  :
  999.  
  1000.  
  1001.  
  1002.  ****************************************************************************/
  1003.  
  1004. void
  1005. tv_update (TIMEVAL * tv, LONG msecs)
  1006. {
  1007.   LONG t_secs, t_msecs;
  1008.   TIMEVAL tv_tmp = *tv;
  1009.   BOOL flag = TRUE;
  1010.  
  1011.   if (msecs < 0)
  1012.     {
  1013.       flag = FALSE;
  1014.       msecs = -msecs;
  1015.     }
  1016.   t_secs = msecs / 1000;
  1017.   t_msecs = msecs % 1000;
  1018.  
  1019.   /*
  1020.      * conv microsecs field to msecs
  1021.    */
  1022.   tv_tmp.tv_micro %= 1000;
  1023.  
  1024.   if (flag == TRUE)
  1025.     {
  1026.       /* add */
  1027.  
  1028.       tv_tmp.tv_micro += t_msecs;
  1029.       if (tv_tmp.tv_micro >= 1000)
  1030.     {
  1031.       tv_tmp.tv_micro %= 1000;
  1032.       tv_tmp.tv_secs += 1;
  1033.     }
  1034.       tv_tmp.tv_secs += t_secs;
  1035.     }
  1036.   else
  1037.     {
  1038.       /* subtract */
  1039.  
  1040.       if (tv_tmp.tv_micro < t_msecs)
  1041.     {
  1042.       tv_tmp.tv_micro += 1000 - t_msecs;
  1043.       tv_tmp.tv_secs -= 1;
  1044.     }
  1045.       tv_tmp.tv_secs -= t_secs;
  1046.     }
  1047.   /*
  1048.      * conv  msecs back to microsecs
  1049.    */
  1050.   tv_tmp.tv_micro *= 1000;
  1051.   /*
  1052.      * update given TIMEVAL
  1053.    */
  1054.  
  1055.   *tv = tv_tmp;
  1056. }
  1057.  
  1058. /***************************************************************************
  1059.  
  1060.  
  1061.  Function :     check4battclock
  1062.  
  1063.  Purpose:       Inits other stuff
  1064.  
  1065.  Entry    :
  1066.  
  1067.  
  1068.  Returns  :
  1069.  
  1070.  
  1071.  
  1072.  ****************************************************************************/
  1073.  
  1074.  
  1075. BOOL
  1076. check4battclock (void)
  1077. {
  1078.  
  1079.   UtilityBase = (LIBRARY *) OpenLibrary ("diskfont.library", 0L);
  1080.  
  1081.  
  1082.   if (UtilityBase == NULL)
  1083.     {
  1084.       fprintf (stderr, "Can't open diskfont.library\n");
  1085.       return FALSE;
  1086.     }
  1087.  
  1088.   UtilityBase = (LIBRARY *) OpenLibrary ("utility.library", EARLIEST_LIB);
  1089.  
  1090.   if (UtilityBase == NULL)
  1091.     {
  1092.       fprintf (stderr, "Abort: only runs under AmigaDOS 2.04 and above\n");
  1093.       return FALSE;
  1094.     }
  1095.   /*
  1096.      * see if Battery Clock there
  1097.    */
  1098.  
  1099.   BattClockBase = OpenResource (BATTCLOCKNAME);
  1100.   if (BattClockBase == NULL)
  1101.     {
  1102.       fprintf (stderr, "Abort: can't find a Battery Clock\n");
  1103.       return FALSE;
  1104.     }
  1105.   return TRUE;
  1106. }
  1107.  
  1108. /***************************************************************************
  1109.  
  1110.  
  1111.  Function :     get_syncd_battclock_time
  1112.  
  1113.  Purpose:       reads battclock and returns when sec just ticked over
  1114.  
  1115.  Entry    :
  1116.  
  1117.  
  1118.  Returns  :
  1119.  
  1120.  
  1121.  
  1122.  ****************************************************************************/
  1123.  
  1124. void
  1125. get_syncd_battclock_time (TIMEVAL * tv)
  1126. {
  1127.   TIMEVAL tv_tmp;
  1128.  
  1129.   get_battclock_time (&tv_tmp);
  1130.   do
  1131.     {
  1132.       get_battclock_time (tv);
  1133.     }
  1134.   while (tv_tmp.tv_secs == tv->tv_secs);
  1135. }
  1136.  
  1137. /***************************************************************************
  1138.  
  1139.  
  1140.  Function :     get_syncd_system_time
  1141.  
  1142.  Purpose:       reads sysclock and returns when sec just ticked over
  1143.  
  1144.  Entry    :
  1145.  
  1146.  
  1147.  Returns  :
  1148.  
  1149.  
  1150.  
  1151.  ****************************************************************************/
  1152.  
  1153. void
  1154. get_syncd_system_time (TIMEVAL * tv)
  1155. {
  1156.   TIMEVAL tv_tmp;
  1157.  
  1158.   get_system_time (&tv_tmp);
  1159.   do
  1160.     {
  1161.       get_system_time (tv);
  1162.     }
  1163.   while (tv_tmp.tv_secs == tv->tv_secs);
  1164. }
  1165.  
  1166. /***************************************************************************
  1167.  
  1168.  
  1169.  Function :     print_clock
  1170.  
  1171.  Purpose:       prints clock time surrounded by given strings
  1172.  
  1173.  Entry    :
  1174.  
  1175.  
  1176.  Returns  :
  1177.  
  1178.  
  1179.  
  1180.  ****************************************************************************/
  1181.  
  1182. void
  1183. print_clock (char *pre, TIMEVAL * tv, char *post)
  1184. {
  1185.  
  1186.   /*
  1187.      * print results
  1188.    */
  1189.   printf ("%s%s%s", pre, tv2str (tv), post);
  1190.   fflush (stdout);
  1191. }
  1192.  
  1193. /***************************************************************************
  1194.  
  1195.  
  1196.  Function :     display both clocks
  1197.  
  1198.  Purpose:       as the name suggests
  1199.  
  1200.  Entry    :
  1201.  
  1202.  
  1203.  Returns  :
  1204.  
  1205.  
  1206.  
  1207.  ****************************************************************************/
  1208.  
  1209. void
  1210. display_clocks (TIMEVAL * tv_b, TIMEVAL * tv_s)
  1211. {
  1212.  
  1213.   print_clock ("Battery clock time = ", tv_b, "\n");
  1214.   print_clock ("System  clock time = ", tv_s, tv_diff_str (tv_b, tv_s));
  1215.   printf ("\n");
  1216.  
  1217. }
  1218.  
  1219. /***************************************************************************
  1220.  
  1221.  
  1222.  Function :     adjust a clock
  1223.  
  1224.  Purpose:       adjusts either system clock or  battclock by given
  1225.  number of millisecs
  1226.  
  1227.  Entry    :
  1228.  
  1229.  
  1230.  Returns  :
  1231.  
  1232.  
  1233.  
  1234.  ****************************************************************************/
  1235.  
  1236. void
  1237. adjust_clock (LONG msecs, TIMEVAL * tv)
  1238. {
  1239.   static TIMEVAL tv_tmpx;
  1240.  
  1241.   /*
  1242.      * wait until given clock wraps over a second
  1243.    */
  1244.   if (s_flag == TRUE)
  1245.     {
  1246.       get_syncd_system_time (&tv_sysclock);
  1247.       tv_tmpx = tv_sysclock;
  1248.       tv_update (&tv_tmpx, msecs);
  1249.  
  1250.       set_system_time (&tv_tmpx);
  1251.     }
  1252.   else
  1253.     {
  1254.       LONG res;
  1255.       /*
  1256.          * battclock here
  1257.        */
  1258.       get_syncd_battclock_time (&tv_battclock);
  1259.       tv_tmpx = tv_battclock;
  1260.       tv_update (&tv_tmpx, msecs);
  1261.  
  1262.       res = 1000 - (tv_tmpx.tv_micro / 1000);
  1263.       tv_update (&tv_battclock, 1000);
  1264.       ++tv_tmpx.tv_secs;
  1265.  
  1266.       /*
  1267.          * - wait a bit to account for fraction of a sec
  1268.        */
  1269.       pause (res);
  1270.  
  1271.       set_battclock_time (&tv_tmpx);
  1272.     }
  1273.   *tv = tv_tmpx;
  1274.  
  1275. }
  1276.  
  1277. /***************************************************************************
  1278.  
  1279.  
  1280.  Function :     print_old_new
  1281.  
  1282.  Purpose:       as the name suggests
  1283.  
  1284.  Entry    :
  1285.  
  1286.  
  1287.  Returns  :
  1288.  
  1289.  
  1290.  
  1291.  ***************************************************************************/
  1292.  
  1293. void
  1294. print_old_new (char *ptr, TIMEVAL * tv_old, TIMEVAL * tv_new)
  1295. {
  1296.   printf ("Old %s clock time = ", ptr);
  1297.   print_clock ("", tv_old, "\n");
  1298.  
  1299.   printf ("New %s clock time = ", ptr);
  1300.   print_clock ("", tv_new, tv_diff_str (tv_old, tv_new));
  1301.   printf ("\n");
  1302.  
  1303. }
  1304.  
  1305. /***************************************************************************
  1306.  
  1307.  
  1308.  Function :     set_system_time_to_syncd_battclock
  1309.  
  1310.  Purpose:       sets system clock to battclock time when it ticks
  1311.  over a second.
  1312.  
  1313.  Entry    :
  1314.  
  1315.  
  1316.  Returns  :
  1317.  
  1318.  
  1319.  
  1320.  ***************************************************************************/
  1321.  
  1322. LONG
  1323. set_system_time_to_syncd_battclock ()
  1324. {
  1325.   get_syncd_battclock_time (&tv_battclock);
  1326.   get_system_time (&tv_sysclock);
  1327.   set_system_time (&tv_battclock);
  1328.  
  1329.   return (tv_diff (&tv_battclock, &tv_sysclock));
  1330. }
  1331.  
  1332. /***************************************************************************
  1333.  
  1334.  
  1335.  Function :     set_battclock_time_to_syncd_system_time
  1336.  
  1337.  Purpose:       sets battery clock to system time when it ticks
  1338.  over a second.
  1339.  
  1340.  Entry    :
  1341.  
  1342.  
  1343.  Returns  :
  1344.  
  1345.  
  1346.  ****************************************************************************/
  1347.  
  1348. LONG
  1349. set_battclock_time_to_syncd_system_time ()
  1350. {
  1351.   TIMEVAL tv_tmp;
  1352.   LONG diff;
  1353.  
  1354.   /*
  1355.      * we need to mess around a bit to get diff
  1356.      * between batt and system clock because batt clock
  1357.      * is only accurate to 1 sec
  1358.    */
  1359.   get_syncd_battclock_time (&tv_battclock);
  1360.   get_system_time (&tv_tmp);
  1361.   diff = tv_diff (&tv_battclock, &tv_tmp);
  1362.  
  1363.   get_syncd_system_time (&tv_sysclock);
  1364.   set_battclock_time (&tv_sysclock);
  1365.  
  1366.   /*
  1367.      * now correct battclock time to give msecs precision
  1368.      * PROVIDED that diff was in range (<999 secs) else ignore
  1369.      * trying to be precise.
  1370.    */
  1371.   if (diff != -1)
  1372.     {
  1373.       tv_battclock = tv_sysclock;
  1374.       tv_update (&tv_battclock, diff);
  1375.     }
  1376.  
  1377.   return (-diff);
  1378. }
  1379.  
  1380. /***************************************************************************
  1381.  
  1382.  
  1383.  Function :      wait_for_timer
  1384.  
  1385.  Purpose:       Waits until given time expires
  1386.  
  1387.  Entry    :
  1388.  
  1389.  
  1390.  Returns  :
  1391.  
  1392.  
  1393.  
  1394.  ****************************************************************************/
  1395.  
  1396.  
  1397. void
  1398. wait_for_timer (TIMERREQUEST * tr, TIMEVAL * tv)
  1399.  
  1400. {
  1401.   MSGPORT *tp;
  1402.   ULONG flags;
  1403.  
  1404.   tp = tr->tr_node.io_Message.mn_ReplyPort;
  1405.  
  1406.   tr->tr_node.io_Command = TR_ADDREQUEST;
  1407.   tr->tr_time.tv_secs = tv->tv_secs;
  1408.   tr->tr_time.tv_micro = tv->tv_micro;
  1409.   SendIO ((IOREQUEST *) tr);
  1410.  
  1411.  
  1412.   flags = Wait ((1L << tp->mp_SigBit) | SIGBREAKF_CTRL_C);
  1413.   if (flags & SIGBREAKF_CTRL_C)
  1414.     {
  1415.       AbortIO ((IOREQUEST *) tr);
  1416.       WaitIO ((IOREQUEST *) tr);
  1417.       hexit (1);
  1418.     }
  1419. }
  1420.  
  1421. /***************************************************************************
  1422.  
  1423.  
  1424.  Function :      pause
  1425.  
  1426.  
  1427.  Purpose  :      waits for given millisecs
  1428.  ( or until ^C hit )
  1429.  
  1430.  Entry    :
  1431.  
  1432.  
  1433.  Returns  :
  1434.  
  1435.  
  1436.  
  1437.  ****************************************************************************/
  1438.  
  1439.  
  1440. void
  1441. pause (int millisecs)
  1442. {
  1443.   static TIMEVAL tv;
  1444.  
  1445.   tv.tv_secs = millisecs / 1000;
  1446.   tv.tv_micro = (millisecs % 1000) * 1000;
  1447.  
  1448.   wait_for_timer (Timermsg, &tv);
  1449. }
  1450.  
  1451.  
  1452. /***************************************************************************
  1453.  
  1454.  
  1455.  Function :     scan_args
  1456.  
  1457.  Purpose:       scans and validates command line
  1458.  
  1459.  Entry    :
  1460.  
  1461.  
  1462.  Returns  :
  1463.  
  1464.  
  1465.  
  1466.  ****************************************************************************/
  1467.  
  1468.  
  1469.  
  1470. void
  1471. scan_args (int argc, char **argv)
  1472.  
  1473. {
  1474.   char *ptr;
  1475.   char arch;
  1476.   char plusminus;
  1477.   int a = 0;
  1478.   int n = 0;
  1479.   int b = 0;
  1480.   int d = 0;
  1481.   int s = 0;
  1482.   int t = 0;
  1483.   int l = 0;
  1484.   int L = 0;
  1485.  
  1486.  
  1487.   while (--argc)
  1488.     {
  1489.       ptr = *++argv;
  1490.       plusminus = *ptr++;
  1491.       /*
  1492.        * now look for options
  1493.        */
  1494.       if ((plusminus == '-') || (plusminus == '+'))
  1495.     {
  1496.       ++opt_count;
  1497.       arch = *ptr++;
  1498.  
  1499.       switch (arch)
  1500.         {
  1501.  
  1502.         case 'a':        /* adjust clock time */
  1503.           if (a++)
  1504.         {
  1505.           fprintf (stderr, "too many -a options\n");
  1506.           hexit (1);
  1507.         }
  1508.           a_flag = TRUE;
  1509.           if (sscanf (ptr, "%d", &adjust) != 1)
  1510.         {
  1511.           fprintf (stderr, "bad -a option value\n");
  1512.           hexit (1);
  1513.         }
  1514.           if (plusminus == '-')
  1515.         adjust = -adjust;
  1516.  
  1517.           break;
  1518.  
  1519.         case 'b':
  1520.           if (b++)
  1521.         {
  1522.           fprintf (stderr, "too many -b options\n");
  1523.           hexit (1);
  1524.         }
  1525.           b_flag = TRUE;
  1526.           break;
  1527.  
  1528.         case 'd':
  1529.           if (d++)
  1530.         {
  1531.           fprintf (stderr, "too many -d options\n");
  1532.           hexit (1);
  1533.         }
  1534.           d_flag = TRUE;
  1535.           date_ptr = ptr;
  1536.           break;
  1537.  
  1538.         case 't':
  1539.           if (t++)
  1540.         {
  1541.           fprintf (stderr, "too many -t options\n");
  1542.           hexit (1);
  1543.         }
  1544.           t_flag = TRUE;
  1545.           time_ptr = ptr;
  1546.           break;
  1547.  
  1548.         case 's':
  1549.           if (s++)
  1550.         {
  1551.           fprintf (stderr, "too many -s options\n");
  1552.           hexit (1);
  1553.         }
  1554.           s_flag = TRUE;
  1555.           break;
  1556.  
  1557.  
  1558.         case 'h':
  1559.           usage ("");    /* help */
  1560.           hexit (1);
  1561.  
  1562.         case 'l':
  1563.           if (l++)
  1564.         {
  1565.           fprintf (stderr, "too many -l options\n");
  1566.           hexit (1);
  1567.         }
  1568.           l_flag = TRUE;
  1569.           logfile = (*ptr) ? ptr : "LOG_CLOCKTOOL:";
  1570.           if ((fp = fopen (logfile, "a")) == NULL)
  1571.         {
  1572.           fprintf (stderr, "Can't open %s\n", logfile);
  1573.           hexit (1);
  1574.         }
  1575.           break;
  1576.  
  1577.         case 'L':
  1578.           if (L++)
  1579.         {
  1580.           fprintf (stderr, "too many -L options\n");
  1581.           hexit (1);
  1582.         }
  1583.           L_flag = TRUE;
  1584.           logfile = (*ptr) ? ptr : "LOG_CLOCKTOOL:";
  1585.           if ((fp = fopen (logfile, "a")) == NULL)
  1586.         {
  1587.           fprintf (stderr, "Can't open %s\n", logfile);
  1588.           hexit (1);
  1589.         }
  1590.           break;
  1591.  
  1592.         case 'n':        /* put str in log file entry    */
  1593.           if (n++)
  1594.         {
  1595.           fprintf (stderr, "too many -n options\n");
  1596.           hexit (1);
  1597.         }
  1598.           n_flag = TRUE;
  1599.           xstr = ptr;
  1600.           break;
  1601.  
  1602.         default:
  1603.           usage ("unknown option");        /* bad option   */
  1604.           hexit (1);
  1605.  
  1606.         }
  1607.     }
  1608.       else
  1609.     {
  1610.       usage ("not an option");
  1611.       hexit (1);
  1612.     }
  1613.     }
  1614.  
  1615.   if (opt_count == 0)
  1616.     return;
  1617.  
  1618.   /*
  1619.    * now validate options
  1620.    */
  1621.  
  1622.   if (b_flag && s_flag)
  1623.     {
  1624.       usage ("only one -b or -s option");    /* bad option   */
  1625.       hexit (1);
  1626.     }
  1627.  
  1628.   if ((b_flag == FALSE) && (s_flag == FALSE))
  1629.     {
  1630.       usage ("need -b or -s option");    /* bad option   */
  1631.       hexit (1);
  1632.     }
  1633.  
  1634.   if (d_flag || t_flag)
  1635.     {
  1636.       if (d_flag)
  1637.     {
  1638.       if (*date_ptr == '\0')
  1639.         {
  1640.           fprintf (stderr, "need a date string\n");
  1641.           hexit (1);
  1642.         }
  1643.     }
  1644.  
  1645.       if (t_flag)
  1646.     {
  1647.       if (*time_ptr == '\0')
  1648.         {
  1649.           fprintf (stderr, "need a time string\n");
  1650.           hexit (1);
  1651.         }
  1652.     }
  1653.       return;
  1654.     }
  1655.  
  1656.   if (l_flag && L_flag)
  1657.     {
  1658.       usage ("only one -l or -L option");    /* bad option   */
  1659.       hexit (1);
  1660.     }
  1661.  
  1662.   if ((opt_count > 2) && (a_flag == TRUE))
  1663.     {
  1664.       usage ("invalid options with -a option");        /* bad option   */
  1665.       hexit (1);
  1666.     }
  1667.  
  1668.   if ((opt_count > 2) && (n_flag == FALSE))
  1669.     {
  1670.       usage ("need a -n option");    /* bad option   */
  1671.       hexit (1);
  1672.     }
  1673. }
  1674.  
  1675. /***************************************************************************
  1676.  
  1677.  
  1678.  Function :     usage
  1679.  
  1680.  Purpose:       prints invocation parameters
  1681.  
  1682.  Entry    :
  1683.  
  1684.  
  1685.  Returns  :
  1686.  
  1687.  
  1688.  
  1689.  ****************************************************************************/
  1690.  
  1691. void
  1692. usage (char *p)
  1693.  
  1694.  
  1695. {
  1696.   static char *rats[] =
  1697.   {
  1698.     "Usage modes :-",
  1699.     " 1) display both   : clocktool",
  1700.     " 2) set selected",
  1701.     "    from other     : clocktool -[b | s]",
  1702.     " 3) set date/time  : clocktool -[b | s] -d<date> -t<time> ",
  1703.     " 4) adjust         : clocktool -[b | s] +|-a<n>",
  1704.     " 5) log then reset : clocktool -[b | s] -l<file> [-n<str>]",
  1705.     " 6) log  only      : clocktool -[b | s] -L<file> [-n<str>]",
  1706.     "\nOptions:",
  1707.     "    -b         : battery clock",
  1708.     "    -s         : system  clock",
  1709.     "    -d<str>    : set selected clock's date to str (dd-MMM-yy)",
  1710.     "    -t<str>    : set selected clock's time to str (hh:mm[:ss])",
  1711.     "                   (can use either or both d/t options)",
  1712.     "",
  1713.  
  1714.     "    +|-a<n>    : change selected clock by + or - <n> millisecs",
  1715.     "",
  1716.     "    -l<file>   : log time of clock to <file> ; reset from other",
  1717.     "                     (default file =  LOG_CLOCKTOOL:)",
  1718.     "    -L<file>   : log time of clock to <file> ; don't reset from other",
  1719.     "                     (default file =  LOG_CLOCKTOOL:)",
  1720.     "    -n<str>    : append the <str> to log file entry, e.g. \"reboot\"",
  1721.     NULL
  1722.   };
  1723.   int j = 0;
  1724.   char *ptr;
  1725.  
  1726.  
  1727.  
  1728.   if (*p)
  1729.     fprintf (stderr, "Error: %s\n\n", p);
  1730.  
  1731.   printf (
  1732.   "%s: Displays/adjusts/sets times of battery clock and/or system clock\n\n",
  1733.        "ClockTool");
  1734.   printf (
  1735.        "Version %s [%s : %s] written by gduncan@werple.net.au\n\n",
  1736.        VERSION, __DATE__, __TIME__);
  1737.  
  1738.   while (ptr = rats[j++])
  1739.     printf ("%s\n", ptr);
  1740. }
  1741.  
  1742. /* */
  1743.